/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Trust.java
 *
 * Created on Feb 10, 2009, 12:07:09 PM
 */

/**
 *
 * @author Himanshu  Sharma
 */
import java.awt.event.MouseListener ;
import java.awt.event.MouseEvent ;
import java.awt.event.InputEvent ;
import java.util.Random;

public class Trust extends javax.swing.JFrame {
    double money = 5.0, money_passed = 0.0;
    double money_returned = 0.0, money_expected = 0.0, p2_money = 0.0 ;
    double money_Won = 0.0;
    String stage = "1" ;

    Game G;
    
    Trust( double init_money ) {
        this.money = init_money;
        initComponents();
        G = new Game();
        l3.setText( Double.toString(money)) ;
        b1.setVisible(false);
        this.setVisible(true);
        addMouseListener( new Trust.CheckMouse());
        t1.setText("0.0"); t2.setText("0.0"); t3.setText("0.0");
        S1() ;
    }
   
    public Trust() {
        this.money = 5.0;
        initComponents();
        G = new Game();
        l3.setText( Double.toString(money)) ;
        b1.setVisible(false);
        this.setVisible(true);
        addMouseListener( new Trust.CheckMouse());
        t1.setText("0.0"); t2.setText("0.0"); t3.setText("0.0");
        S1() ; 
    }


    @Override
    public synchronized void addMouseListener(MouseListener l) {
        super.addMouseListener(l);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bg1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        l1 = new javax.swing.JLabel();
        l2 = new javax.swing.JLabel();
        l4 = new javax.swing.JLabel();
        t1 = new javax.swing.JTextField();
        l5 = new javax.swing.JLabel();
        l3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        t2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        t3 = new javax.swing.JTextField();
        b2 = new javax.swing.JButton();
        b1 = new javax.swing.JButton();
        l10 = new javax.swing.JLabel();
        l11 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 204, 204));

        l1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        l1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        l1.setText("You are Player 1");

        l2.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        l2.setText("You have $");

        l4.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        l4.setText("What would you like to give to player 2");

        l5.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        l5.setText("How much do you expect the player 2 to pass ?");

        l3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        l3.setText("12");

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Trust");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel3.setText("How much do you think Player 2 Expects you to pass");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(52, 52, 52))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGap(32, 32, 32)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(l4)
                                .addComponent(l5))
                            .addGap(19, 19, 19))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(l2)
                            .addGap(18, 18, 18)
                            .addComponent(l3)
                            .addGap(22, 22, 22))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(t1, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
                            .addComponent(t2, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE))
                        .addGap(74, 74, 74))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(t3, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(377, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addGap(261, 261, 261))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(331, Short.MAX_VALUE)
                .addComponent(l1)
                .addGap(206, 206, 206))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(32, 32, 32)
                .addComponent(l1)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l2)
                    .addComponent(l3))
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l4)
                    .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(l5)
                    .addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(53, 53, 53)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(t3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(40, 40, 40))
        );

        b2.setText("Give");
        b2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b2ActionPerformed(evt);
            }
        });

        b1.setText("Next");
        b1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b1ActionPerformed(evt);
            }
        });

        l10.setBackground(new java.awt.Color(204, 204, 204));
        l10.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        l10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        l10.setText("Left Click to increase value");
        l10.setOpaque(true);

        l11.setBackground(new java.awt.Color(204, 204, 204));
        l11.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        l11.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        l11.setText("Right Click when done.");
        l11.setOpaque(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(497, 497, 497)
                        .addComponent(b2)
                        .addGap(142, 142, 142)
                        .addComponent(b1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(177, 177, 177)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(l11, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(l10, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(45, 45, 45)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(b1)
                            .addComponent(b2)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(75, 75, 75)
                        .addComponent(l10, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(26, 26, 26)
                        .addComponent(l11, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(91, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b2ActionPerformed
        money_passed = Double.parseDouble(t1.getText()) ;
        money_expected = Double.parseDouble(t3.getText()) ;
        p2_money = 3 * money_passed;
        Random r = new Random();
        double frac_mon_ret = 0;
        frac_mon_ret = r.nextDouble(); // We'll use random values to find the money returned. Out of 10, 6 will be no money.
        money_returned = money_passed * frac_mon_ret;
        money_Won = money - money_passed + money_returned;
        l3.setText(Double.toString(money));
        b2.setVisible(false);
        b1.setVisible(true);
}//GEN-LAST:event_b2ActionPerformed

    private void b1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b1ActionPerformed
       G.writeLog("Trust_Player1_money_won $" + String.format("%.2f", money_Won) +
                ", Initial_Money $" + String.format("%.2f", money )+
                ", Money_Passed: $" + String.format("%.2f", money_passed) +
                ", Money_Expected_from_P2 $" + String.format("%.2f",money_expected) +
                ", P2_passed_money: $" + String.format("%.2f",money_returned ) ) ;
        setVisible(false) ;
        G.free = true;
    }//GEN-LAST:event_b1ActionPerformed



    class CheckMouse implements MouseListener{
        public void mousePressed(MouseEvent e) {}
        public void mouseReleased(MouseEvent e) {}
        public void mouseEntered(MouseEvent e) {}
        public void mouseExited(MouseEvent e) {}
        public void mouseClicked(MouseEvent e) {
            int modifiers = e.getModifiers();
            if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
            if( stage.equals("1") && t1.isEditable()) {
                double temp = Double.parseDouble(t1.getText()) + 1.0 ;
                t1.setText(Double.toString(temp));
            }
            else if( stage.equals("2") && t2.isEditable()) {
                double temp = Double.parseDouble(t2.getText()) + 1.0 ;
                t2.setText(Double.toString(temp));
            }
            else if( stage.equals("3") && t3.isEditable() ){
                double temp = Double.parseDouble(t3.getText()) + 1.0 ;
                t3.setText(Double.toString(temp));
            }
            else if( stage.equals("4")){
                stage = "5" ;
                l10.setText("You have finished this Game");
                l11.setText("Press left Mouse key to move to Next Game");
                money_passed = Double.parseDouble(t1.getText()) ;
                money_expected = Double.parseDouble(t3.getText()) ;
                p2_money = 3 * money_passed;
                Random r = new Random();
                double frac_mon_ret = 0;
                frac_mon_ret = r.nextDouble(); // We'll use random values to find the money returned. Out of 10, 6 will be no money.
                money_returned = money_passed * frac_mon_ret;
                money_Won = money - money_passed + money_returned;
                l3.setText(Double.toString(money));
                b2.setVisible(false);
                b1.setVisible(true);

            }
            else if( stage.equals("5") ){
                G.writeLog("Trust_Player1_money_won $" + String.format("%.2f", money_Won) +
                     ", Initial_Money $" + String.format("%.2f", money )+
                     ", Money_Passed: $" + String.format("%.2f", money_passed) +
                     ", Money_Expected_from_P2 $" + String.format("%.2f",money_expected) +
                     ", P2_passed_money: $" + String.format("%.2f",money_returned ) ) ;
                setVisible(false) ;
                G.free = true;
            }
            }
            if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {}
            if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
                if( stage.equals("1") ) {
                    stage = "2" ;
                    S2() ;
                }
                else if( stage.equals("2") ) {
                    stage = "3" ;
                    S3() ;
                }
                else if( stage.equals("3") ) {
                    stage = "4" ;
                    S4() ; 
                }
            }
        }
    }
    /**
    * @param args the command line arguments
    */

    public void S1(){
        t1.setVisible(true) ; 
        t2.setVisible(false);
        t3.setVisible(false);
        t1.setEditable(true);
        t2.setEditable(false);
        t3.setEditable(false);
        l4.setVisible(true) ;
        l5.setVisible(false) ;
        jLabel3.setVisible(false) ;
    }

    public void S2(){
        t1.setVisible(false) ;
        t2.setVisible(true);
        t3.setVisible(false);
        t1.setEditable(false);
        t2.setEditable(true);
        t3.setEditable(false);
        l4.setVisible(false) ;
        l5.setVisible(true) ;
        jLabel3.setVisible(false) ;
        l2.setVisible(false) ;
        l3.setVisible(false) ;
    }

    public void S3(){
        t1.setVisible(false) ;
        t2.setVisible(false);
        t3.setVisible(true);
        t1.setEditable(false);
        t2.setEditable(false);
        t3.setEditable(true);
        l4.setVisible(false) ;
        l5.setVisible(false) ;
        jLabel3.setVisible(true) ;
        l2.setVisible(false) ;
        l3.setVisible(false) ;
    }

    public void S4(){
        t1.setVisible(false) ;
        t2.setVisible(false);
        t3.setVisible(false);
        t1.setEditable(false);
        t2.setEditable(false);
        t3.setEditable(false);
        l4.setVisible(false) ;
        l5.setVisible(false) ;
        jLabel3.setVisible(false) ;
        l2.setVisible(false) ;
        l3.setVisible(false) ;
        l10.setText("Press Left Mouse Button to Send Money");
        l11.setText("");

    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Trust().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton b1;
    private javax.swing.JButton b2;
    private javax.swing.ButtonGroup bg1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel l1;
    private javax.swing.JLabel l10;
    private javax.swing.JLabel l11;
    private javax.swing.JLabel l2;
    private javax.swing.JLabel l3;
    private javax.swing.JLabel l4;
    private javax.swing.JLabel l5;
    private javax.swing.JTextField t1;
    private javax.swing.JTextField t2;
    private javax.swing.JTextField t3;
    // End of variables declaration//GEN-END:variables

}
